package com.ikonke.iot.alarm.rest;

import com.ikonke.iot.alarm.es.EsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 14:54 2020/6/10
 * @Modified By:
 */
@RestController
public class AlarmDataController {

    @Autowired
    private EsRepository esRepository;

    //    @GetMapping("download")
    //    public void testDownload(@RequestParam String code, HttpServletResponse response) {
    //        String fileName = "异常统计信息" + ".txt";
    //        String content = JSON.toJSONString(esRepository.getStatisticCcuId(), true);
    //        String content1 = JSON.toJSONString(esRepository.getStatisticOpcode(), true);
    //        response.setContentType("application/octet-stream");
    //        try {
    //            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
    //        } catch (UnsupportedEncodingException e) {
    //            e.printStackTrace();
    //        }
    //        ServletOutputStream outputStream = null;
    //        BufferedOutputStream buffer = null;
    //        try {
    //            outputStream = response.getOutputStream();
    //            buffer = new BufferedOutputStream(outputStream);
    //            if ("ccuid".equalsIgnoreCase(code)) {
    //                buffer.write(content.getBytes("UTF-8"));
    //            } else if ("opcode".equalsIgnoreCase(code)) {
    //                buffer.write(content1.getBytes("UTF-8"));
    //            }
    //            buffer.flush();
    //            buffer.close();
    //            outputStream.close();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //    }

    @GetMapping("/getErrorInfo")
    public String getErrorInfo(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        esRepository.statErrorMsg(date, new String[] { "1", "2" });
        return "SUCCESS";
    }
}
